Image Transformations in Pytorch¶

  • In this notebook, more advanced transformations using PyTorch's torchvision.transforms.

  • The following transformations are applied to an example image.

  1. ToTensor: Convert the image to a PyTorch tensor.
  2. Normalize: Normalize pixel values to have zero mean and unit variance.
  3. Center Crop: Crop the image from the center.
  4. Horizontal Rotation: Rotate the image horizontally.
  5. Horizontal Flip: Flip the image horizontally.
  6. Grayscale: Convert the image to grayscale.
  7. Brightness: Adjust the brightness of the image.
  8. Hue: Adjust the hue of the image.
  9. Saturation: Adjust the saturation of the image.
  10. Sharpness: Adjust the sharpness of the image.
  11. Histogram Equalization: Enhance the image's contrast using histogram equalization.
  • The Image used is this one with JPEG format,size :(4288, 2848) and RGB model.

    Alt Text
In [ ]:
from PIL import Image
import torch 
import torchvision
import matplotlib.pyplot as plt
In [ ]:
"""Load Image"""

# Open the image file
img = Image.open('image.jpg')

# View img properties
print('Image Size',img.size)
print('Image Mode',img.mode)
print('Image Format',img.format)
# Show the image
img.show()
Image Size (640, 427)
Image Mode RGB
Image Format JPEG
In [ ]:
"""Image to Tensor """
# Set tranformation
transform = torchvision.transforms.Compose([
    torchvision.transforms.ToTensor(),
])


# Apply transformation to image
tensor_image = transform(img) 

# Print tensor shape
print(tensor_image.shape)  # (Channels, Height ,Width)
torch.Size([3, 427, 640])
In [ ]:
"""Normalize Image"""
# Numbers represent Channels
# torchvision.transforms.Normalize([mean_1, mean_2, mean_3], [std_1, std_2, std_3])
#Example:
torchvision.transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))

# ApplyTransformation 
normalized = transform(img)
# View Img

# Transpose tensor to (height, width, channels) order
tensor_image = normalized.permute(1, 2, 0)

# Convert tensor to numpy array
numpy_image = tensor_image.numpy()

# Display image using matplotlib
plt.imshow(numpy_image)
Out[ ]:
<matplotlib.image.AxesImage at 0x7ff882113f70>
In [ ]:
""" Center Crop """
# Tranformation
transform = torchvision.transforms.CenterCrop((400,400))
# Apply tranform
cropped= transform(img)
# View Img
cropped.size
plt.imshow(cropped)
Out[ ]:
<matplotlib.image.AxesImage at 0x7ff87efde7f0>
In [ ]:
"""Random Horizontal Flip"""

transform = torchvision.transforms.RandomHorizontalFlip(p=0.9)
transformed= transform(img)

plt.imshow(transformed)
Out[ ]:
<matplotlib.image.AxesImage at 0x7ff87ef94b80>
In [ ]:
"""RandomRotation"""

transform = torchvision.transforms.RandomRotation(degrees=30)
transformed = transform(img)
plt.imshow(transformed)
Out[ ]:
<matplotlib.image.AxesImage at 0x7ff87edc4700>
In [ ]:
"""Color : Grayscale"""

transform = torchvision.transforms.Grayscale(num_output_channels=1)
grayscale = transform(img)

plt.imshow(grayscale)
Out[ ]:
<matplotlib.image.AxesImage at 0x7ff87ee9f670>
In [ ]:
""" Gaussian Blurr """

transform = torchvision.transforms.GaussianBlur(kernel_size=501)
blurred = transform(img)

plt.imshow(blurred)
Out[ ]:
<matplotlib.image.AxesImage at 0x7ff87eb849a0>
In [ ]:
""" Brightness"""
bright = torchvision.transforms.functional.adjust_brightness(img,brightness_factor=3.8)

plt.imshow(bright)
Out[ ]:
<matplotlib.image.AxesImage at 0x7ff87ea59d60>
In [ ]:
""" Contrast """
contrasted = torchvision.transforms.functional.adjust_contrast(img,contrast_factor=5.8)
plt.imshow(contrasted)
Out[ ]:
<matplotlib.image.AxesImage at 0x7ff87e6f51f0>
In [ ]:
""" Hue """
hued = torchvision.transforms.functional.adjust_hue(img,hue_factor=0.5)
plt.imshow(hued)
Out[ ]:
<matplotlib.image.AxesImage at 0x7ff87e565430>
In [ ]:
""" Sharpness """
sharp = torchvision.transforms.functional.adjust_sharpness(img,sharpness_factor=8)
plt.imshow(sharp)
Out[ ]:
<matplotlib.image.AxesImage at 0x7ff87e4ea850>
In [ ]:
""" Histogram Equalizing """
equal = torchvision.transforms.functional.equalize(img)
plt.imshow(equal)
Out[ ]:
<matplotlib.image.AxesImage at 0x7ff87e959cd0>